package com.example.dell.fastshopping.views;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dell.fastshopping.R;

/**
 * Created by ${YSH} on 2019/4/20.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class NumberView extends RelativeLayout {

    private TextView num;
    private int number = 0;
    private int numberDiff = 200;

    public NumberView(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public NumberView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public NumberView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.number_view, this, false);

        num = (TextView) inflate.findViewById(R.id.num);
        num.setText(number + "");
        inflate.findViewById(R.id.cut).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                number = number - numberDiff;
                if (number <= 0) {
                    num.setText("0");
                } else {
                    num.setText(number + "");
                }
            }
        });


        inflate.findViewById(R.id.add).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                number = number + numberDiff;
                if (number < 0) {
                    number=0;
                    num.setText("0");
                } else {
                    num.setText(number + "");
                }
            }
        });
        this.addView(inflate);
    }

    public Integer getNumber() {
        String s = num.getText().toString().trim();
        return Integer.valueOf(s);
    }

    public void setNumberDiff(int numberDiff) {
        if (numberDiff <= 0) {
            this.numberDiff = 100;
        } else {
            this.numberDiff = numberDiff;
        }
    }

    public void setNumber(int lunfinalPrice) {

        num.setText(lunfinalPrice + "");
        number =lunfinalPrice;
    }
}
